﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYRevitCode.FormUtility;

namespace YArchitech.HVAC
{
	public partial class ConnPipeFrom : System.Windows.Forms.Form
	{
		public ConnectPipeStyle PipeConn { get; set; }

		public bool Flag
		{
			get
			{
				return this.flag;
			}
			set
			{
				this.flag = value;
			}
		}

		public ConnPipeFrom()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			List<KeyValuePair<System.Windows.Forms.Control, bool>> inputControlGroup = new List<KeyValuePair<System.Windows.Forms.Control, bool>>();
			this.m_thisManger = new ControlInfoManager(this, inputControlGroup, "{1EFF9D1D-AB7F-4FC6-A2F7-AFD3BE7330CB}", null);
			base.Icon = Resource.YArchitechIcon;
		}

		private void ConnPipeFrom_Load(object sender, EventArgs e)
		{
			if (this.m_thisManger != null && !this.m_thisManger.SetControlInfoToForm())
			{
				base.Location = new System.Drawing.Point(230, 170);
			}
			if (this.userSetting.GetRevitPeizhi() == "0")
			{
				this.cbdefault.Checked = false;
			}
			else
			{
				this.cbdefault.Checked = true;
			}
			this.InitialButton();
		}

		private void InitialButton()
		{
			string pipeConnStyle = this.userSetting.GetPipeConnStyle();
			if (pipeConnStyle == "Elbow")
			{
				this.SelectButton(this.btnElbow);
				return;
			}
			if (pipeConnStyle == "Tee")
			{
				this.SelectButton(this.btnTee);
				return;
			}
			if (pipeConnStyle == "Cross")
			{
				this.SelectButton(this.btnCross);
				return;
			}
			if (pipeConnStyle == "Reduce")
			{
				this.SelectButton(this.btnReduce);
				return;
			}
			if (!(pipeConnStyle == "Back"))
			{
				return;
			}
			this.SelectButton(this.btnBack);
		}

		private void SelectButton(Button button)
		{
			button.PerformClick();
			button.Select();
			button.Focus();
		}

		private void btnConn_Click(object sender, EventArgs e)
		{
			System.Drawing.Color backColor = this.gbStyle.BackColor;
			Button button = sender as Button;
			if (button == null)
			{
				return;
			}
			string name = button.Name;
			if (!(name == "btnElbow"))
			{
				if (!(name == "btnTee"))
				{
					if (!(name == "btnCross"))
					{
						if (!(name == "btnBack"))
						{
							if (name == "btnReduce")
							{
								this.PipeConn = ConnectPipeStyle.Reduce;
							}
						}
						else
						{
							this.PipeConn = ConnectPipeStyle.Back;
						}
					}
					else
					{
						this.PipeConn = ConnectPipeStyle.Cross;
					}
				}
				else
				{
					this.PipeConn = ConnectPipeStyle.Tee;
				}
			}
			else
			{
				this.PipeConn = ConnectPipeStyle.Elbow;
			}
			foreach (object obj in this.gbStyle.Controls)
			{
				Button button2 = obj as Button;
				if (button2 != null)
				{
					button2.BackColor = backColor;
					if (button2 == button)
					{
						button2.BackColor = System.Drawing.Color.LightSkyBlue;
					}
				}
			}
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void ConnPipeFrom_FormClosed(object sender, FormClosedEventArgs e)
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			ConnPipeFrom.SendMessage(HYPipeFittingConn.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			ConnPipeFrom.keybd_event((byte)bVk, 0, dwFlags, 0);
			ConnPipeFrom.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void ConnPipeFrom_Leave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(Cursor.Position))
			{
				int wMsg = 7;
				ConnPipeFrom.SendMessage(HYPipeFittingConn.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void ConnPipeFrom_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(Cursor.Position))
			{
				int wMsg = 7;
				ConnPipeFrom.SendMessage(HYPipeFittingConn.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void ConnPipeFrom_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void ConnPipeFrom_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_thisManger != null)
			{
				this.m_thisManger.GetControlInfoToXml();
			}
			this.userSetting.WritePipeConnStyle(this.PipeConn.ToString());
		}

		private void cbdefault_CheckedChanged(object sender, EventArgs e)
		{
			if (this.cbdefault.Checked)
			{
				this.flag = true;
				this.btnBack.Enabled = false;
				this.userSetting.WriteRevitPeizhi("1");
				if (this.btnBack.BackColor == System.Drawing.Color.LightSkyBlue)
				{
					this.btnConn_Click(this.btnElbow, null);
					return;
				}
			}
			else
			{
				this.flag = false;
				this.btnBack.Enabled = true;
				this.userSetting.WriteRevitPeizhi("0");
			}
		}

		private HVACUserSetting userSetting = new HVACUserSetting();

		private bool flag;

		private ControlInfoManager m_thisManger;
	}
}
